﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PocketBook.Domain.Abstract;
using PocketBook.UI.Model;
using System.Collections.Specialized;

namespace PocketBook.UI.ViewModel.Workspaces
{
    public class StatusWorkspaceViewModel : WorkspaceViewModel
    {
        private MainWindowViewModel _parent;
        private IModelFactory _model;

        #region Constructor

        public StatusWorkspaceViewModel(MainWindowViewModel parent)
        {
            _parent = parent;
            _model = ViewModelLocator.Kernel.Get<ModelFactory>();
            base.DisplayName = "Status";
            this.Init();
            _parent.ExpensesWorkspace.ExpensesList.CollectionChanged += OnExpensesListCollectionChanged;
            _parent.IncomesWorkspace.IncomesList.CollectionChanged += OnIncomesListCollectionChanged;
        }

        #endregion

        #region Status

        private const string StatusPropertyName = "Status";
        private decimal _status;

        public decimal Status
        {
            get
            {
                return _status;
            }
            set
            {
                if (_status == value)
                {
                    return;
                }
                _status = value;
                RaisePropertyChanged(StatusPropertyName);
            }
        }

        #endregion

        #region LastMonthExpenses

        private const string LastMonthExpensesPropertyName = "LastMonthExpenses";
        private decimal _lastMonthExpenses;

        public decimal LastMonthExpenses
        {
            get
            {
                return _lastMonthExpenses;
            }
            set
            {
                if (_lastMonthExpenses == value)
                {
                    return;
                }
                _lastMonthExpenses = value;
                RaisePropertyChanged(LastMonthExpensesPropertyName);
            }
        }

        #endregion

        #region LastMonthIncomes

        private const string LastMonthIncomesPropertyName = "LastMonthIncomes";
        private decimal _lastMonthIncomes;

        public decimal LastMonthIncomes
        {
            get
            {
                return _lastMonthIncomes;
            }
            set
            {
                if (_lastMonthIncomes == value)
                {
                    return;
                }
                _lastMonthIncomes = value;
                RaisePropertyChanged(LastMonthIncomesPropertyName);
            }
        }

        #endregion

        #region Private Helpers

        private void Init()
        {
            Status = _model.GetPocketBookStatus();
            LastMonthExpenses = _model.GetPocketBookLastMonthExpenses();
            LastMonthIncomes = _model.GetPocketBookLastMonthIncomes();
        }

        #endregion

        #region Event Handlers

        private void OnExpensesListCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            this.Init();
        }

        private void OnIncomesListCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            this.Init();
        }

        #endregion
    }
}
